// TokenTable.cpp: implementation of the TokenTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Expression.h"
#include "TokenTable.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TokenTable theTokenTable;

TokenTable& GetTokenTable()
{
	return theTokenTable;
}

TokenTable::TokenTable()
{

}

TokenTable::~TokenTable()
{
	ClearTable();
}

Variable* TokenTable::GetToken(const CString& strToken)
{
	Variable* pVar = NULL;
	if(m_theTokenTable.Lookup(strToken, pVar))
		return pVar;

	pVar = new Variable;
	pVar->Symbol(strToken);
	AddToken(pVar);
	return pVar;
}

void TokenTable::AddToken(Variable* pVariable)
{
	Variable* pVar = NULL;
	if(m_theTokenTable.Lookup(pVariable->GetSymbol(), pVar))
		return;
	m_theTokenTable.SetAt(pVariable->GetSymbol(), pVariable);
}

void TokenTable::ClearTable()
{
	POSITION pos;
	CString strKey;
	Variable* pVar = NULL;

	pos = m_theTokenTable.GetStartPosition();
	while(pos)
	{
		m_theTokenTable.GetNextAssoc(pos, strKey, pVar);
		delete pVar;
	}
	
}